/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package umled.model;
import java.util.*;
import java.awt.Graphics2D;
import org.w3c.dom.*;
/**
 *
 * @author ANor
 */
public class UMLMethod extends UMLComponent{
    private List<UMLType> params;
    private boolean isStatic;
    private boolean isAbstract;
    private UMLType returnType;
    
    public UMLMethod(String name, UMLElement parent, int xPos, int yPos, int width, int height, UMLVisible visible,
            UMLType returnType, List<UMLType> params, boolean isStatic, boolean isAbstract)
    {
       super(name, parent, xPos, yPos, width, height, visible);
        this.returnType = returnType;
        this.params = params;
        this.isAbstract = isAbstract;
        this.isStatic = isStatic;
    }
    public List<UMLType> getParams(){
        if( params != null)
            return Collections.unmodifiableList( params );
        return null;
    }
    public boolean isStatic(){
        return isStatic;
    }
    public void setStatic(boolean isStatic){
        this.isStatic = isStatic;
    }
    public UMLType getReturnType(){
        return returnType;
    }
    public boolean isAbstract(){
        return isAbstract;
    }
    public void setAbstract(boolean isAbstract){
        this.isAbstract = isAbstract;
    }
    public void draw(Graphics2D g2d, int x, int y){
        drawRectangle( g2d, getX(), getY(), getWidth(), getHeight(), x, y);
        drawString( g2d, toString(), getX() + 10, getY() + 10, x, y);
    }
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append( getVisible()).append( getName()).append("( ");
        for( int i=0 ; i < params.size() - 1 ; i++ )
            sb.append( params.get(i) ).append(", ");
        sb.append( params.get( params.size() - 1 )).append("):").append( returnType );
        return sb.toString();
    }
    @Override
    public Element getXML( Document document ){
        Element element = super.getXML( document );
        element.setAttribute( "type", "UMLMethod" );
        
        Element temp = document.createElement( "Params List");
        for( UMLType type : params )
            temp.appendChild( document.createTextNode( type.getName() ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Return Type" );
        temp.appendChild( document.createTextNode( returnType.getName() ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Is Static" );
        temp.appendChild( document.createTextNode( String.valueOf( isStatic() ) ) );
        element.appendChild( temp );
        
        temp = document.createElement( "Is Abstract" );
        temp.appendChild( document.createTextNode( String.valueOf( isAbstract() ) ) );
        element.appendChild( temp );
        
        return element;
    }
}
